package cn.genmer.test.security.jackcess;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.healthmarketscience.jackcess.ColumnBuilder;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Table;
import com.healthmarketscience.jackcess.TableBuilder;
public class AccessUtil {
	public static void main(String[] args) throws IOException {
		//这里同样支持mdb和accdb
		Database db = DatabaseBuilder.create(Database.FileFormat.V2000, new File("D:\\geocloud\\mdbdownloads\\new.mdb"));
		Table newTable;
		try {
			//刚才是创建文件，这里是在文件里创建表，字段名，字段类型
			newTable = new TableBuilder("Archives")
			.addColumn(new ColumnBuilder("档案号")
			.setSQLType(Types.INTEGER))
			.addColumn(new ColumnBuilder("编制单位")
			.setSQLType(Types.VARCHAR))
			.addColumn(new ColumnBuilder("案卷正题名")
			.setSQLType(Types.VARCHAR))
			.addColumn(new ColumnBuilder("案卷题目长度")
			.setSQLType(Types.INTEGER))
			.addColumn(new ColumnBuilder("档案盒规格")
			.setSQLType(Types.VARCHAR))
			.addColumn(new ColumnBuilder("编制单位长度")
			.setSQLType(Types.INTEGER))
			.toTable(db);
			//插入一条数据测试
			newTable.addRow("12", "foo","212",44,"323",56);
		}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void operator() throws Exception {
		File mdbFile = new File("D:\\mdbdownloads\\new.mdb");
		if (mdbFile.exists()) {
			Database dbin = DatabaseBuilder.open(mdbFile);
			Table table = dbin.getTable("Archives");
			table.addRow("档案号 新增测试", "编制单位新增测试", "案卷正题目新增测试",55, "档案盒规格测试", 5);
		}
	}

	public void batchAdd() throws IOException, SQLException {
		Database db = DatabaseBuilder.create(Database.FileFormat.V2000, new File("D:\\mdbdownloads\\new.mdb"));
		Table newTable = new TableBuilder("table")
				.addColumn(new ColumnBuilder("列名1")
						.setSQLType(Types.VARCHAR)
						.toColumn())
				.addColumn(new ColumnBuilder("列名2")
						.setSQLType(Types.VARCHAR)
						.toColumn())
				.addColumn(new ColumnBuilder("列名3")
						.setSQLType(Types.VARCHAR)
						.toColumn())
				.toTable(db);
		ArrayList<Object[]> list=new ArrayList<Object[]>();
		for (int j = 0; j < 10000; j++){
			Object[] obj={"第一列的值: " + j,"第二列的值: " + j,(j+1)};
			list.add(obj);
		}
		newTable.addRows(list);
	}
}